package com.jian.mall.produce.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.jian.mall.produce.entity.AttrEntity;
import com.jian.mall.produce.service.AttrAttrgroupRelationService;
import com.jian.mall.produce.service.AttrService;
import com.jian.mall.produce.service.CategoryService;
import com.jian.mall.produce.vo.AttrGroupRelationVo;
import com.jian.mall.produce.vo.AttrGroupWithAttrsVo;
import com.jian.mall.produce.vo.AttrVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.*;

import com.jian.mall.produce.entity.AttrGroupEntity;
import com.jian.mall.produce.service.AttrGroupService;
import com.jian.mall.common.utils.PageUtils;
import com.jian.mall.common.utils.R;


/**
 * 属性分组
 *
 * @author tainanle
 * @email 12345678@gmail.com
 * @date 2022-08-10 22:48:11
 */
@RestController
@RequestMapping("produce/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;
    @Autowired
    @Qualifier("categoryService")
    private CategoryService categoryService;
    @Autowired
    private AttrService attrService;
    @Autowired
    private AttrAttrgroupRelationService attrAttrgroupRelationService;


    // 获取分类下所有分组&关联属性 /{categoryId}/withattr
    // http://139.9.34.27:10010/produce/attrgroup/225/withattr
    @GetMapping("/{categoryId}/withattr")
    public R getAttrGroupWithAttrs(@PathVariable("categoryId")Long catId){
        List<AttrGroupWithAttrsVo> vos = attrGroupService.getAttrGroupWithAttrsByCatId(catId);
        return R.ok().put("data",vos);
    }


    // /1/attr/relation
    @GetMapping("/{groupId}/attr/relation")
    public R attrRelation(@PathVariable("groupId") Long groupId) {
        List<AttrEntity> attrVo = attrService.selectListByGroupId(groupId);

        return R.ok().put("data", attrVo);
    }

    @GetMapping("/{groupId}/noattr/relation")
    public R noAttrRelation(@PathVariable("groupId") Long groupId,
                          @RequestParam Map<String, Object> params) {
        PageUtils pageUtils = attrService.selectListNotRelation(params, groupId);
        return R.ok().put("page",pageUtils);
    }

    @PostMapping("/attr/relation")
    public R addRelation(@RequestBody AttrGroupRelationVo[] attrGroupRelationVos) {
        attrAttrgroupRelationService.saveBatch(attrGroupRelationVos);
        return R.ok();
    }

    // /attr/relation/delete
    @PostMapping("/attr/relation/delete")
    public R relationDelete(@RequestBody AttrGroupRelationVo[] attrGroupRelationVos) {
        attrService.deleteByAttrIdAndRelationIds(attrGroupRelationVos);
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/list/{catelogId}")
    //@RequiresPermissions("produce:attrgroup:list")
    public R list(@RequestParam Map<String, Object> params, @PathVariable("catelogId") Long catelogId) {
//        PageUtils page = attrGroupService.queryPage(params);
        PageUtils page = attrGroupService.queryPage(params, catelogId);
        return R.ok().put("page", page);
    }

    /**
     * 获取所有
     */
    @RequestMapping("/list")
    //@RequiresPermissions("produce:attrgroup:list")
    public R listAll(@RequestParam Map<String, Object> params) {
        PageUtils page = attrGroupService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    //@RequiresPermissions("produce:attrgroup:info")
    public R info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        if (attrGroup == null) {
            return R.error();
        }
        Long[] path = categoryService.findCategoryPath(attrGroup.getCatelogId());
        attrGroup.setCatelogIdPath(path);
        return R.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("produce:attrgroup:save")
    public R save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("produce:attrgroup:update")
    public R update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("produce:attrgroup:delete")
    public R delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return R.ok();
    }

}
